package com.hson.demo.base.crypter;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.hson.demo.base.util.StringUtil;


/**
 * AES加解密工具类
 * 
 * @author huangym3
 * @time 2016年3月7日 下午4:02:34
 */
public class WechatCrypterUtil {

	private WechatCrypterUtil() {
	}

	private static final String KEY = "huangym3c5e81ac7507011e5a29a989096dd4427@by-health.com";

	public static String encode(String content) {
		return new String(Base64.encodeBase64(encrypt(content)));
	}

	public static String encode(String content, String password) {
		return new String(Base64.encodeBase64(encrypt(content, password)));
	}

	public static byte[] encrypt(String content) {
		return encrypt(content, KEY);
	}

	public static byte[] encrypt(String content, String password) {
		try {
			// 实例化一个用AES加密算法的密钥生成器
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(password.getBytes());
			// 使用用户提供的password初始化此密钥生成器，使其具有确定的密钥大小256字节长。
			kgen.init(256, random);
			// 生成一个密钥。
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			// 根据给定的enCodeFormat字节数组构造一个用AES算法加密的密钥。
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			// 创建密码器
			Cipher cipher = Cipher.getInstance("AES");
			// 使用给定的 utf-8编码将此 String 编码到 byte 序列，并将结果存储到byteContent 数组。
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);// 以加密的方式用密钥初始化此 Cipher
			// 加密 返回加密过后的byteContent
			byte[] result = cipher.doFinal(byteContent);
			return result;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String decode(String content) {
		return new String(decrypt(Base64.decodeBase64(content)));
	}
	
	public static String decode(String content, String password) {
		return new String(decrypt(Base64.decodeBase64(content), password));
	}
	
	public static byte[] decrypt(byte[] content) {
		return decrypt(content, KEY);
	}

	public static byte[] decrypt(byte[] content, String password) {
		try {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(password.getBytes());
			kgen.init(256, random);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
			byte[] result = cipher.doFinal(content);
			return result; // 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		String encodeStr = encode("帅哥啊");
		System.out.println(encodeStr);
		String decodeStr = decode(encodeStr);
		System.out.println(decodeStr);
		
		encodeStr = encode("帅哥啊", "abcdefghijklmn");
		System.out.println(encodeStr);
		decodeStr = decode(encodeStr, "abcdefghijklmn");
		System.out.println(decodeStr);

		encodeStr = encode("帅哥啊帅哥啊帅哥啊帅哥啊", "abcdefghijklmn");
		System.out.println(encodeStr);
		decodeStr = decode(encodeStr, "abcdefghijklmn");
		System.out.println(decodeStr);

		encodeStr = encode("帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊", "abcdefghijklmn");
		System.out.println(encodeStr);
		decodeStr = decode(encodeStr, "abcdefghijklmn");
		System.out.println(decodeStr);

		encodeStr = encode("帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊帅哥啊", "abcdefghijklmn");
		System.out.println(encodeStr);
		decodeStr = decode(encodeStr, "abcdefghijklmn");
		System.out.println(decodeStr);
		
		String randomStr = StringUtil.getRandomString(32);
		System.out.println("random string:" + randomStr);
		String randomStrEncode = encode(randomStr);
		System.out.println("encode string:" + randomStrEncode);
		System.out.println("decode string:" + decode(randomStrEncode));
	}
}
